package com.agolansoft.quickpicturetaker;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class QuickPictureTaker extends Activity implements SurfaceHolder.Callback {
	private Camera camera;
	private boolean isPreviewRunning = false;
	private boolean mTakePic = true;
	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        Log.e(getClass().getSimpleName(), "onCreate");
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.main);
        surfaceView = (SurfaceView)findViewById(R.id.svQuickLookup);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    
	/*
	protected void onResume() {
		Log.e(getClass().getSimpleName(), "AVI4 111 (OPTIONAL!!!) ");
		camera.startPreview();
		super.onResume();
	}*/

	protected void onSaveInstanceState(Bundle outState)
	{
	super.onSaveInstanceState(outState);
	}
	
	public void surfaceCreated(SurfaceHolder holder)
	{
		Log.e(getClass().getSimpleName(), "surfaceCreated");
		Log.e(getClass().getSimpleName(), "AVI6 111");
		camera = Camera.open();
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		Log.e(getClass().getSimpleName(), "AVI7 222");
		Log.e(getClass().getSimpleName(), "surfaceChanged");
		if (mTakePic){
			camera.stopPreview();
			camera.setDisplayOrientation(90);
			camera.takePicture(null, mPictureCallback, mPictureCallback);
			mTakePic = false;
		}
		if (isPreviewRunning) {
			camera.stopPreview();
		}
		Camera.Parameters p = camera.getParameters();
		p.setPreviewSize(w, h);
		camera.setParameters(p);
		try {
			camera.setPreviewDisplay(holder);
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		camera.startPreview();
		isPreviewRunning = true;
	}
	
	Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
		public void onPictureTaken(byte[] imageData, Camera c) {
			Log.e(getClass().getSimpleName(), "AVI Saving IMAGE....");
			if (imageData != null) {
				if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
					String fileName = Environment.getExternalStorageDirectory() + createName() + ".jpg";
					File imageFile = new File(fileName);
					try {
						BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(imageFile));
						bos.write(imageData);
						bos.flush();
						bos.close();
					} 
					catch (IOException e) {
						e.printStackTrace();
					}
				}
				else {
					Log.e(getClass().getSimpleName(), "SDCARD is not Accessible...");
				}
				camera.startPreview();
				finish();
			}
		}
	};

	public void surfaceDestroyed(SurfaceHolder holder)
	{
		 Log.e(getClass().getSimpleName(), "AVI8 END 111");
		Log.e(getClass().getSimpleName(), "surfaceDestroyed");
		camera.stopPreview();
		isPreviewRunning = false;
		camera.release();
	}
	
	protected void onStop()
	{
		Log.e(getClass().getSimpleName(), "onStop");
		Log.e(getClass().getSimpleName(), "AVI5 END 222");
		super.onStop();
	}
	
    private String createName() {
    	long dateTaken = System.currentTimeMillis();
        Date date = new Date(dateTaken);
        SimpleDateFormat dateFormat = new SimpleDateFormat("y_MM_d_m_s");

        return "qpt_" + dateFormat.format(date);
    }
}